package com.example.administrator.capital.login;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.capital.MainActivity;
import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseActivity;
import com.example.administrator.capital.model.bean.LoginBean;
import com.example.administrator.capital.presenter.LoginPresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.CommonUtils;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.LogUtils;
import com.example.administrator.capital.utils.PreferenceManager;
import com.example.administrator.capital.utils.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;

/*账号登录界面*/
public class LoginActivity extends BaseActivity {
    @BindView(R.id.phone)
    AutoCompleteTextView mPhone;
    @BindView(R.id.password)
    EditText mPassword;
    @BindView(R.id.btn_eye)
    CheckBox mBtnEye;
    @BindView(R.id.login)
    Button mLogin;
    @BindView(R.id.login_fast)
    TextView mLoginFast;
    @BindView(R.id.register_fast)
    TextView mRegisterFast;
    @BindView(R.id.forget)
    TextView mForget;
    private String phone;
    private LoginPresenter mLoginPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }
    @Override
    public void initView() {
        mLoginPresenter = new LoginPresenter(null);
        isPasswordShowListener();

    }

    @Override
    public void setListener() {
        mLogin.setOnClickListener(this);
        mForget.setOnClickListener(this);
        mLoginFast.setOnClickListener(this);
        mRegisterFast.setOnClickListener(this);

    }

    @Override
    public void setData() {

    }

    /**
     * @param username
     * @param password
     * 登录
     */
    private void  Login(String username, String password) {
        mLoginPresenter.getSign(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }

            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }

            @Override
            public void onSuccess(Response<String> data) {
                LoginBean bean = JsonUtils.parse(data.body(), LoginBean.class);
                PreferenceManager.instance().saveToken(bean.getData().getLogintoken());
                PreferenceManager.instance().saveUseId(String.valueOf(bean.getData().getUid()));
                showActivity(MainActivity.class,null);
                finish();
            }

            @Override
            public void onFail(Response<String> error) {

            }
        },username,password);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                if (isPhoneNumber()) {
                    Login(mPhone.getText().toString(),mPassword.getText().toString());
                }

                break;
            case R.id.login_fast:
                showActivity(FastLoginActivity.class,null);
                break;
            case R.id.forget:
                showActivity(ForgetActivity.class,null);
                break;
            case R.id.register_fast:
                showActivity(RegisterActivity.class,null);
                break;
        }
    }
    /**
     * @return
     * 判断手机号
     */
    private boolean isPhoneNumber() {
        phone = mPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(mContext, "手机号不能为空!", Toast.LENGTH_SHORT).show();
            return false;
        } else if (CommonUtils.isMobileNO(phone)) {
            return true;
        } else {
            Toast.makeText(mContext, "手机号不合法!", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
    /*显示密码*/
    private void isPasswordShowListener() {
        mBtnEye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = mPassword.getText().length();
                mPassword.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                mPassword.setSelection(passwordLength);
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
